package com.hzlh.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 异常统一处理
 * @Author: linker_zhang
 * @Date: Created in 2020/5/13 19:18
 * @Version:1.0.0
 */
@Slf4j
@RestControllerAdvice(annotations = Controller.class)
public class HandlerExceptionAdvice {

    @ExceptionHandler(value = Exception.class)
    public Object exceptionHandler(HttpServletRequest request, Exception ex) {
        LOGGER.error(ex.getMessage());
        LOGGER.error("An exception occurred.", ex);
        return ResponseGenerator.error();
    }

    @ExceptionHandler(value = GlobalException.class)
    public Object globalExceptionHandler(HttpServletRequest request, GlobalException ex) {
        LOGGER.error("A GlobalException occurred.Request Address：[{}].Error Msg：[{}]", request.getRequestURI(), ex.getResponseCode().getMsg());
//        return ResponseGenerator.error(ex.getResponseCode());
        return ResponseGenerator.generate(ex.getResponseCode(), ex.getData());
    }

}
